package com.share.tool.devops.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Result;
import com.share.common.utils.HttpUtil;
import com.share.tool.devops.enums.UrlGCodeMenu;
import com.share.tool.devops.model.po.HttpRequestLog;
import com.share.tool.devops.model.po.HttpUrlConfig;
import com.share.tool.devops.model.req.HttpUrlConfigListReq;
import com.share.tool.devops.service.HttpRequestLogService;
import com.share.tool.devops.service.HttpUrlConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/health")
public class HealthController {
    @Autowired
    private HttpUrlConfigService httpUrlConfigService;

    @Autowired
    private HttpRequestLogService httpRequestLogService;

    private static List<HttpUrlConfig> nodeList;

    @PostConstruct
    public void initConfig() {
        HttpUrlConfigListReq req = new HttpUrlConfigListReq();
        req.setGCode(UrlGCodeMenu.HEALTH_CHECK.code);
        nodeList = httpUrlConfigService.listConfig(req);
    }

    @Scheduled(fixedRate = 10000)
    public void nodeBeat() {
        if (CollectionUtils.isEmpty(nodeList)) {
            log.info("集合为空");
        }

        for (HttpUrlConfig item : nodeList) {
            //初始化存储
            int dealStatus = 1;
            String param = null;
            long startTime = System.currentTimeMillis();
            Integer logId = httpRequestLogService.insertLog(item, dealStatus, param);
            if (logId == null) {
                log.info("入库异常");
                continue;
            }

            //发起http请求
            String result = null;
            try {
                result = HttpUtil.get(item.getUrl());
                JSONObject jsonObject = JSON.parseObject(result);
                if ("UP".equals(jsonObject.get("status"))) {
                    dealStatus = 2;
                } else {
                    dealStatus = 3;
                }
            } catch (Exception e) {
                log.info("请求异常e={}", e.getMessage());
                dealStatus = 4;
            }

            //结果入库
            HttpRequestLog logRecord = new HttpRequestLog();
            logRecord.setId(logId);
            logRecord.setStatus(dealStatus);
            logRecord.setUpdateTime(new Date());

            String reqParam = null;
            int index = item.getUrl().indexOf("?");
            if (index > -1) {
                reqParam = item.getUrl().substring(index);
            }
            logRecord.setRequestParam(reqParam);
            logRecord.setRespondData(result);
            logRecord.setProcessTime(System.currentTimeMillis() - startTime);
            httpRequestLogService.updateById(logRecord);
        }
    }

    @ResponseBody
    @RequestMapping("/list")
    public Result<List<HttpUrlConfig>> list(HttpUrlConfigListReq req) {
        return Result.success(httpUrlConfigService.listConfig(req));
    }
}
